package com.xwdeshj.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xwdeshj.dal.Dal_XwfarmAtlasinfo;
import com.xwdeshj.entity.XwFarmAtlasinfo;
import com.xwdeshj.repository.XwFarmAtlasinfoRepository;
import com.xwdeshj.service.XwFarmAtlasinfoService;
import com.xwdeshj.utils.helper;
import lombok.extern.slf4j.Slf4j;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by BKWJA on 2019/1/1.
 * 农家乐——农家图集信息 实现类
 */
@Slf4j
@Service("xwFarmAtlasinfoService")
public class XwFarmAtlasinfoServiceImpl implements XwFarmAtlasinfoService {

    @Autowired
    private XwFarmAtlasinfoRepository xwFarmAtlasinfoRepository;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private Sid sid;

    Dal_XwfarmAtlasinfo dal_xwfarmAtlasinfo = new Dal_XwfarmAtlasinfo();


    /**
     * 新增农家乐图集
     * @param parmJson  farm_encryptId:农家乐加密id，imglink：图片链接url
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @Modifying
    public JSONObject savefarmImgurl(String parmJson,HttpServletRequest request) {
        JSONObject result = new JSONObject();
        String uid_encryptid = helper.ReadCookieMap(request,"uid_encryptid").toString();
        String uid = helper.ReadCookieMap(request,"uid").toString();
        if(!"".equals(uid_encryptid) && !"".equals(uid)){
            if(!parmJson.equals("")){
                JSONArray jsonArray = new JSONArray();
                jsonArray = JSON.parseArray(parmJson);
                for (int i=0 ; i < jsonArray.size() ; i++){
                    JSONObject object = jsonArray.getJSONObject(i);
                    String id = object.getString("id");
                    String business_encryptId = object.getString("business_encryptId");
                    String farm_encryptId = object.getString("farm_encryptId");
                    String imglink = object.getString("imglink");
                    if (imglink.equals("")){
                        //删除
                        XwFarmAtlasinfo xwFarmAtlasinfo = new XwFarmAtlasinfo();
                        xwFarmAtlasinfo.setBusinessEncryptid(business_encryptId);
                        xwFarmAtlasinfo.setFarmEncryptid(farm_encryptId);
                        xwFarmAtlasinfo.setFarmAtlasId(Integer.parseInt(id));
                        boolean temp = xwFarmAtlasinfoRepository.exists(Example.of(xwFarmAtlasinfo));
                        if(temp){
                            xwFarmAtlasinfoRepository.deleteByFarmAtlasId(Integer.parseInt(id));
                        }
                        continue;
                    }else{
                        JSONArray array =  dal_xwfarmAtlasinfo.getfarmAtlasinfo(business_encryptId,farm_encryptId,
                                id,jdbcTemplate);
                        if (array.size() == 0){
                            XwFarmAtlasinfo xwFarmAtlasinfo = new XwFarmAtlasinfo();
                            xwFarmAtlasinfo.setBusinessEncryptid(business_encryptId);
                            xwFarmAtlasinfo.setFarmEncryptid(farm_encryptId);
                            xwFarmAtlasinfo.setFarmAtlasImg(imglink);
                            xwFarmAtlasinfo.setFarmAtlasCreatetime(new Date());
                            xwFarmAtlasinfo.setFarmAtlasUpdatetime(new Date());
                            xwFarmAtlasinfoRepository.save(xwFarmAtlasinfo);
                        }else {
                            XwFarmAtlasinfo farmAtlasinfo = new XwFarmAtlasinfo();
                            farmAtlasinfo.setFarmAtlasId(Integer.parseInt(id));
                            boolean temp = xwFarmAtlasinfoRepository.exists(Example.of(farmAtlasinfo));
                            if (temp){
                                XwFarmAtlasinfo xwFarmAtlasinfo = xwFarmAtlasinfoRepository.findByFarmAtlasId(Integer.parseInt(id));
                                xwFarmAtlasinfo.setFarmAtlasImg(imglink);
                                xwFarmAtlasinfo.setFarmAtlasUpdatetime(new Date());
                                xwFarmAtlasinfoRepository.save(xwFarmAtlasinfo);
                            }
                        }
                    }
                }

                result.put("status",0);
                result.put("msg","ok");
            }else {
                result.put("status",40001);
                result.put("msg","uid不能为空,请先登录！");
            }
        }else {
            result.put("status",40001);
            result.put("msg","uid不能为空,请先登录！");
        }
        System.out.println("savefarmImgurl:"+result.toString());
        return result;
    }


    /**
     * 弹框中获取对应店铺下的 图片图集
     */
    @Override
    public JSONObject getfarmaltlasinfowin(String farm_encryptId, HttpServletRequest request) {
        JSONObject result = new JSONObject();
        if(!"".equals(farm_encryptId)){
            result = dal_xwfarmAtlasinfo.getfarmaltlasinfowin(farm_encryptId,jdbcTemplate);
        }
        System.out.println("getfarmaltlasinfowin:"+result.toString());
        return result;
    }













    public Dal_XwfarmAtlasinfo getDal_xwfarmAtlasinfo() {
        return dal_xwfarmAtlasinfo;
    }

    public void setDal_xwfarmAtlasinfo(Dal_XwfarmAtlasinfo dal_xwfarmAtlasinfo) {
        this.dal_xwfarmAtlasinfo = dal_xwfarmAtlasinfo;
    }
}
